<?php

namespace cms\modules\v1\models\im;

use cms\modules\v1\models\user\User;
use common\models\Functions;
use common\models\goods\GoodsTagRelation;
use common\models\group\GroupUser;
use common\models\Status;
use yii\base\Exception;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class ImGroupUser extends GroupUser
{

    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getListData($page = 1, $size = 20) {
        try {
            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件
            //得到文章的总数
            $count = static::getObjectCountByAndWhere($searchWhere['where'], $searchWhere['andWhere'])->count();
            $data = static::setPageinationAPI($page, $size, $count);
            //设置分页
            $offset = ($page - 1) * $size;

            //排序 order
            $orderSql = 'id ASC';
//            $orderSql = $this->getOrder();

            $query = static::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);
            $data['data'] = $this->handelList($query);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    public function getOrder()
    {
        $sort = Yii::$app->request->get('sort');

        $sort = json_decode($sort, true);

        if (empty($sort) || $sort['order'] == "normal") {
            return "id ASC";
        }
        $field = $sort['field'];

        return $field . " " . $sort['order'];

    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     */
    public function getSearch () {
        $where = [];
        $andWhere = [];
        $phone = Yii::$app->request->get('phone','');
        $shutUp = Yii::$app->request->get('shut_up','');
        $identity = Yii::$app->request->get('identity','');
        $startTime = Yii::$app->request->get('start', '');
        $endTime = Yii::$app->request->get('end', '');
        $groupId = Yii::$app->request->get('group_id', 0);

        if ($phone) {
            $andWhere[] = ['like','user_phone',$phone];
        }


        if ($shutUp) {
            $where['is_shut_up'] = $shutUp;
        }

        if ($identity) {
            $where['user_type'] = $identity;
        }

        if ($groupId) {
            $where['group_id'] = $groupId;
        }

        if ($startTime && $endTime) {
            $andWhere[] = ['between', 'create_time', $startTime, $endTime];
        }

        return [
            'where' => $where,
            'andWhere' => $andWhere,
        ];
    }

    /**
     * 处理初始化数据
     * @param $query
     * @return mixed
     */
    public function handelList($query = null) {
        if (empty($query)) {
            return null;
        }

        $list = $query->with([
            'user' => function ($q) {
                $q->select(['id','login_name','user_face','nick_name','tx_code','user_code','money']);
            },

        ])->asArray()->all();

        foreach ($list as &$v) {
            $singUserInfo = ArrayHelper::getValue($v,'user',[]);


            $v['user_id'] = $singUserInfo['tx_code'] ?: '';
            $v['phone'] = $singUserInfo['login_name'] ?: '';
            $v['user_face'] = static::getAvatar($singUserInfo['user_face']);
            $v['nick_name'] = $singUserInfo['nick_name'] ?: '';
            $v['money'] = floatval($singUserInfo['money']);

        }
        return $list;
    }


    public function getMapForSelect() {
        $list = static::getByWhere();
        $result = [];
        foreach ($list as $v) {
            $result[] = [
                "id" => $v['id'],
                'name' => $v['name']
            ];
        }
        return Functions::formatRestAPI(0, '', $result);
    }


}
